﻿@Imports System
@Imports System.Data
@Imports System.Collection.Generics
@Imports System.Reflection
@Imports System.Web
@Imports System.Web.Mvc


@Helper FormatSectionAlpha(ByVal cat As String, ByVal SectionTable As Object)
    Dim TileBgcolor = "orange"
    Dim TileTemplate = "app"
    Dim TileURL = ""
    Dim TileIcon = ""
    Dim TileAppLabel = ""
    Dim TileAppCount = ""

    For j = 97 To 97 + 25
        Dim count As Int32 = SectionTable.Select("str_key LIKE '" & Chr(j) & "%'").Length
        If count > 0 Then
            TileBgcolor = "orange"
            If cat = "titles" Then
                TileURL = "/List/" & cat & "/" & Chr(j)
            Else
                TileURL = "/Browse/" & cat & "/" & Chr(j)
            End If
        Else
            TileBgcolor = "darken"
            TileURL = "/Browse/" & cat & "/#"
        End If

        TileIcon = "icon-alpha_" & Chr(j)
        TileAppCount = count
            
    @<a href="@Href(TileURL)" class='tile @TileTemplate bg-color-@TileBgcolor'>
        <div class="image-wrapper">
            <span class="icon @TileIcon"></span>
        </div>
        <div class="app-label">
            @TileAppLabel
        </div>
        <div class="app-count">
            @count
        </div>
    </a>
Next
End Helper


@Helper FormatSection(ByVal cat As String, ByVal RowsFound As Object, ByVal itemindex As Integer, ByVal CatRoot As Array, ByVal bgcolor As Array, sql_str_count As String)

    Dim _sql_str_where() As String = sql_str_Class.Get_sql_str_where
    For row = 0 To RowsFound.GetUpperBound(0)
        Try
            Dim strvalue As String = ""
            Dim strlookupkey As String = ""
            Dim TileBgcolor = "blue"
            Dim count As String
            If itemindex > 0 Then
                TileBgcolor = bgcolor(itemindex)
            Else
                TileBgcolor = "blue"
            End If
            Dim TileTemplate = "app"
            Dim TileURL = ""
            Dim TileIcon = ""
            Dim TileAppLabel = ""
            Dim TileAppCount = ""
            Dim url As String = "/List/" & cat
            If cat = "usercat" Then
                Dim strURL = RowsFound(row)(3)
                Dim StrArray() = Split(strURL, "/")
                If UBound(StrArray) > 0 Then
                    If StrArray(1) = "List" Then
                        strlookupkey = StrArray(2) ' cat type
                        strvalue = StrArray(3) ' name of the tag/serie...
                    End If
                Else
                    strlookupkey = "usercat" ' cat type
                End If

                If strlookupkey <> "usercat" Then
                    Dim countindex As Int32 = Array.IndexOf(CatRoot, strlookupkey)
                    sql_str_count = "SELECT COUNT(books.id) AS Expr1 FROM books " & _sql_str_where(countindex)

                End If

                TileURL = RowsFound(row)(3)

                If strlookupkey = "usercat" Then
                    TileIcon = "icon-user_cat"
                    count = ""
                    TileBgcolor = "blue"
                Else
                    TileIcon = "icon-" & strlookupkey
                    count = LibCalibreVB.ExecuteScalar(String.Format(sql_str_count, strvalue))
                    TileBgcolor = bgcolor(Array.IndexOf(CatRoot, strlookupkey))
                End If
            Else
                Select Case cat
                    Case "authors", "publisher", "series", "tags"
                        TileURL = url & "/" & RowsFound(row)(0) & "/" & LibCalibreVB.StripSlug(RowsFound(row)(1))
                   
                    Case Else
                        TileURL = url & "/" & RowsFound(row)(0)
                End Select

                TileIcon = "icon-" & cat
                If cat = "titles" Then
                    count = 1
                Else
                    count = LibCalibreVB.ExecuteScalar(String.Format(sql_str_count, RowsFound(row)(0)))
                End If
            End If
            If cat = "rating" Then
                If RowsFound(row)(1) = 0 Then
                    TileAppLabel = "(not yet rated)"
                Else
                    TileAppLabel = RowsFound(row)(1) / 2
                End If
            Else
                TileAppLabel = RowsFound(row)(1)
            End If
            TileAppCount = count
            If count <> "0" Or strlookupkey = "usercat" Then
    @<a href="@Href(String.Format("~/{0}", TileURL))" class='tile @TileTemplate bg-color-@TileBgcolor'>
        <div class="image-wrapper">
            <span class="icon @TileIcon"></span>
        </div>
        <div class="app-label">
            @TileAppLabel
        </div>
        <div class="app-count">
            @count
        </div>
    </a>
End If
Catch ex As Exception

End Try
Next
End Helper


@Helper FormatHome(ByVal Table As DataTable, ByVal CatRoot As Array, ByVal bgcolor As Array)
    Dim i = 0
    Dim TileTemplate = "app"
    Dim TileBgcolor = "blue"
    Dim TileURL = ""
    Dim totalbooks As Integer = LibCalibreVB.ExecuteScalar("SELECT Count (id) FROM books")
    @<div class="">
        @For Each _catype As String In CatRoot
            If Not i > 1 Then
                TileTemplate = "wide app"
            Else
                TileTemplate = "app"
            End If
            TileBgcolor = bgcolor(i)
            If i = 0 And totalbooks < ConfigurationManager.AppSettings("CatSize") Then
                TileURL = "/List/" & _catype.ToLower
            Else
                TileURL = "/Browse/" & _catype.ToLower
            End If
            @<a href="@Href(String.Format("~/{0}", TileURL))" class='tile @TileTemplate bg-color-@TileBgcolor'>
                <div class="image-wrapper">
                    <span class="icon icon-@_catype"></span>
                </div>
                <div class="app-label">
                    @_catype
                </div>
            </a>
            i = i + 1
            If i = 9 Then i = 0
        Next
        <a href="@Href("~/List/")" class='tile app bg-color-blue'>
            <div class="image-wrapper">
                <span class="icon icon-happy"></span>
            </div>
            <div class="app-label">
                @Html.Raw("What's New")
            </div>
        </a>
    </div>
    Dim expression As String = "[Parent] = 0 AND CatType = 'usercat'"
    Dim UserCats() As DataRow
    UserCats = Table.Select(expression)
    i = 8
    @<div class="">
        @For j = 0 To UserCats.GetUpperBound(0)
            Dim tilesusercat As New Tile()
            If i > 7 Then
                TileTemplate = "wide app"
            Else
                TileTemplate = "app"
            End If
            TileBgcolor = bgcolor(i)
            TileURL = UserCats(j)(3)
            @<a href="@Href(String.Format("~/{0}", TileURL))" class='tile @TileTemplate bg-color-@TileBgcolor'>
                <div class="image-wrapper">
                    <span class="icon icon-user_cat"></span>
                </div>
                <div class="app-label">
                    @UserCats(j)(1)
                </div>
            </a>
            i = i - 1
            If i = 0 Then i = 8
        Next
    </div>
End Helper


@Helper FormatString(ByVal column As String, ByVal value As String)
    If Not value = String.Empty Then
        If value = "0" Then value = "(not yet rated)"
    @<p><b>@column</b>: @Html.Raw(value)</p>
    End If
End Helper

@Helper FormatHyperlink(ByVal column As String, ByVal value As String)
    
    If Not value = String.Empty Then
        Dim printvalue = value
        If column = "Rating" Then
            If value = "0" Then
                printvalue = "(not yet rated)"
            Else
                value = value.AsInt * 2
            End If
            'Response.Write(value)
        End If
    @<p><b>@column</b>: <a href="@Href("~/List", column, value)">@printvalue</a></p>
End If
 
End Helper


@Helper FormatTable(ByVal column As String, ByVal table As Object, ByVal id As String)
    If table.rows.count > 0 Then
    @<p>
        <b>@column</b>:
            @For Each f In table
                    Select Case column
                        Case "Formats"
                @<a href="@Href("~/File", id, f.format)">@f.format</a>
                        Case "IDs"
                            Select Case f.type
                                Case "amazon"
                @<a href="@(String.Format("http://amazon/{0}", f.val))">@f.type</a>
                                Case "google"
                @<a href="@(String.Format("http://books.google.com/books?id={0}", f.val))">@f.type</a>
                                Case "isbn"
                @<a href="@(String.Format("http://www.worldcat.org/isbn/{0}", f.val))">@f.type</a>
                                Case "barnesnoble"
                @f.type
                                Case Else
                @f.type
                            End Select
                        Case "Tags"
                @<a class="btn btn-mini" href="@Href(String.Format("~/List/" & column & "/{0}/{1}", f.id, LibCalibreVB.StripSlug(f.name)))">@f.name</a>
                        Case Else
                @<text>[<a href="@Href(String.Format("~/List/" & column & "/{0}/{1}", f.id, LibCalibreVB.StripSlug(f.name)))">@f.name</a>]</text>
                    End Select
                Next f
    </p> 
End If
End Helper

@Helper Pagination(ByVal str_path As String, ByVal _PageCount As Integer, _currentpageindex As Integer, ButtonsCount As Integer)
    If _PageCount <= 1 Then
        
    Else
        Dim pagestart As Integer = _currentpageindex - (_currentpageindex Mod ButtonsCount)
        Dim pageend As Integer = _currentpageindex + (ButtonsCount - (_currentpageindex Mod ButtonsCount))
        Dim q = ""
        If Not Request.QueryString("q").IsEmpty() Then
            q = "&q=" & Server.UrlEncode(Trim(Request.QueryString("q")))
        End If
        Dim ii As Integer = 0
    @<ul>
        @If pagestart > ButtonsCount - 1 Then
            @<li><a href="@Href(String.Format(str_path & "/?p=1" & q))" class=''>First</a></li>
            @<li><a href="@Href(String.Format(str_path & "/?p=" & pagestart & q))" class=''>...</a></li>
            End If
        @For ii = pagestart To pageend - 1
                If ii < _PageCount Then
                    If ii = _currentpageindex Then
            @<li><a href="@Href(String.Format(str_path & "/?p=" & (ii + 1).ToString() & q))" style='font-weight:bold'>@(ii + 1)</a></li>
                    Else
            @<li><a href="@Href(String.Format(str_path & "/?p=" & (ii + 1).ToString() & q))" class=''>@(ii + 1)</a></li>
                    End If
                End If
            Next
        @If _PageCount > pageend Then
            @<li><a href="@Href(String.Format(str_path & "/?p=" & ii + 1 & q))" class=''>...</a></li>
            @<li><a href="@Href(String.Format(str_path & "/?p=" & _PageCount & q))" class=''>Last</a></li>
            End If
    </ul>
End If
End Helper

@Helper FormatCover(ByVal title As String, ByVal has_cover As Boolean, ByVal id As String, Optional ByVal size As String = "large")

    @<a href="@Href(String.Format("~/Details/{0}/{1}", id, LibCalibreVB.StripSlug(title)))">

        @If has_cover Then
            @<img class="img-rounded" alt="@Html.AttributeEncode(title)" src="@Href("~/Photo", id, New With {.size = size})" />
        Else
            @<img alt="@title" src="@Href("~/Content/img/book-cover-default.png")" />
        End If
    </a>
 
End Helper

@Helper FormatFormats(ByVal table As Object, ByVal id As String)
    If table.rows.count > 0 Then
        For Each f In table
    @<a class="icon-formats" href="@Href("~/File", id, f.format)"></a>
    @<a href="@Href("~/File", id, f.format)">@f.format</a>
    @<br />
Next f
End If
End Helper

@Helper FormatTileAuthors(ByVal cat As String, ByVal table As DataTable, ByVal id As String)

    If table.Rows.Count > 0 Then
        Dim position = 0
        If table.Rows.Count > 1 Then position = 0
        For a = 0 To position
    @<a href="@Href(String.Format("~/List/{0}/{1}/" & LibCalibreVB.StripSlug(table.Rows(a)(0)), cat, table.Rows(a)(1)))" class='tile app bg-color-red'>
        <div class="image-wrapper">
            <span class="icon icon-authors"></span>
        </div>
        <div class="app-label">
            @Html.Raw(table.Rows(a)(0))
        </div>
    </a>
Next a
End If
End Helper

@Helper FormatTileAuthorsSlider(ByVal cat As String, ByVal table As DataTable, ByVal id As String)
    If table.Rows.Count > 0 Then
        If table.Rows.Count = 1 Then
    @<a href="@Href(String.Format("~/List/{0}/{1}/" & LibCalibreVB.StripSlug(table.Rows(0)(0)), cat, table.Rows(0)(1)))" class='tile app bg-color-red'>
        <div class="image-wrapper">
            <span class="icon icon-authors"></span>
        </div>
        <div class="app-label">
            @Html.Raw(table.Rows(0)(0))
        </div>
    </a>
Else
    Dim position = 0
    @<div class='tile app bg-color-red' data-role="tile-slider" data-param-period="3000">
        @For Each dr As DataRow In table.Rows
            @<div class="tile-content">
                <a href="@Href(String.Format("~/List/{0}/{1}/" & LibCalibreVB.StripSlug(table.Rows(position)(0)), cat, table.Rows(position)(1)))" >
                    <div class="image-wrapper">
                        <span class="icon icon-authors"></span>
                    </div>
                    <div class="app-label">
                        @Html.Raw(table.Rows(position)(0))
                    </div>
                </a>
            </div>
                    position = position + 1
                Next
    </div>
End If
End If
End Helper


@Helper FormatTileFormats(ByVal table As DataTable, ByVal id As String)

    If table.Rows.Count > 0 Then
        Dim position = 0
        If table.Rows.Count > 1 Then position = 0
        For a = 0 To position
    @<a href="@Href("~/File", id, table.Rows(a)(0))" class='tile app bg-color-greenDark'>
        <div class="image-wrapper">
            <span class="icon icon-formats"></span>
        </div>
        <div class="app-count">
            @Html.Raw(table.Rows(a)(0))
        </div>
    </a>
Next a
End If
End Helper



@Helper FormatTileFormatsSlider(ByVal table As DataTable, ByVal id As String)

    If table.Rows.Count > 0 Then
        If table.Rows.Count = 1 Then
            
    @<a href="@Href("~/File", id, table.Rows(0)(0))" class='tile app bg-color-greenDark'>
        <div class="image-wrapper">
            <span class="icon icon-formats"></span>
        </div>
        <div class="app-count">
            @Html.Raw(table.Rows(0)(0))
        </div>
    </a>
            
Else
    Dim position = 0
    Dim value As Integer = CInt(Int((9 * Rnd()) + 1)) * 100 + 2000

    @<div class='tile app bg-color-greenDark' data-role="tile-slider" data-param-period="@value" data-param-direction="left">
        @For Each dr As DataRow In table.Rows
            @<div class="tile-content">
                <a href="@Href("~/File", id, table.Rows(position)(0))">
                    <div class="image-wrapper">
                        <span class="icon icon-formats"></span>
                    </div>
                    <div class="app-count">
                        @Html.Raw(table.Rows(position)(0))
                    </div>
                </a>
            </div>
                    position = position + 1
                Next
    </div>
End If
End If
End Helper


@Helper FormatTileRating(ByVal cat As String, ByVal id As String)
    Dim ratingsTableAdapter As New CalibreVBDataSetTableAdapters.ratingsbybookTableAdapter()
    Dim rating As Object = ratingsTableAdapter.ScalarGetRatingsByBook(id)
    If Not (rating Is Nothing) Then
    @<a href="@Href(String.Format("~/List/{0}/{1}", cat, rating))" class='tile app bg-color-yellow'>
        <div class="image-wrapper">
            <span class="icon icon-rating"></span>
        </div>
        @If rating = "0" Then
            @<div class="app-label">
                @Html.Raw(Resources.MainResources.not_yet_rated)
            </div>
            Else
            @<div class="app-count">
                @Html.Raw(rating / 2)
            </div>
            End If
    </a>
End If
End Helper

@Helper FormatTileSeries(ByVal cat As String, ByVal id As String)
    Dim SeriesTableAdapter As New CalibreVBDataSetTableAdapters.seriesbybookTableAdapter()
    Dim seriesid As String = CType(SeriesTableAdapter.ScalarGetSeriesIdByBook(id), String)
    Dim seriesname As String = CType(SeriesTableAdapter.ScalarGetSeriesNameByBook(id), String)
    If Not (seriesid Is Nothing) Then
    @<a href="@Href(String.Format("~/List/{0}/{1}/{2}", cat, seriesid, seriesname))" class='tile app bg-color-purple'>
        <div class="image-wrapper">
            <span class="icon icon-series"></span>
        </div>
        <div class="app-label">
            @Html.Raw(seriesname)
        </div>
    </a>@<br />
End If
End Helper

@Helper FormatTileSquarePeek(ByVal title As String, ByVal has_cover As Boolean, ByVal id As String, Optional ByVal t As String = "")

    @<a href="@Href(String.Format("~/Details/{0}/{1}", id, LibCalibreVB.StripSlug(title)))"  class="tile squarepeek ">

        @If has_cover Then
            @<img alt="@title" src="@Href("~/Photo/" & id & "/" & t)" />
        Else
            @<img alt="@title" src="@Href("~/Content/img/book-cover-default.png")" />
        End If

        <div class="text-inner">
            <div class="text-header">
            </div>
            <div class="text8">
                @title
            </div>
        </div>
    </a>

End Helper

@Helper FormatTileSquarePeekSlider(ByVal title As String, ByVal has_cover As Boolean, ByVal id As String, Optional ByVal t As String = "")

    Dim value As Integer = CInt(Int((9 * Rnd()) + 1)) * 100 + 4000

    @<a href="@Href(String.Format("~/Details/{0}/{1}", id, LibCalibreVB.StripSlug(title)))" class="tile squarepeekc" data-role="tile-slider" data-param-period="@value">
        <div class="tile-content">
            @If has_cover Then
                @<img alt="@title" src="@Href("~/Photo/" & id & "/" & t)" class="img-rounded" />
            Else
                @<img alt="@title" src="@Href("~/Content/img/book-cover-default.png")" />
            End If
        </div>
        <div class="tile-content">
            <div class="text-inner">
                <div class="text-header">
                    <br />
                    <br />
                    <br />
                    <br />
                </div>
                <div class="text8">
                    @title
                </div>
            </div>
        </div>
    </a>

End Helper



@helper FormatTileString(ByVal id As String, ByVal sourceUrl As String, ByVal controls As Boolean)

    Dim builder As New TagBuilder("tilestring")
    builder.MergeAttribute("id", id)
    builder.MergeAttribute("src", sourceUrl)
    If controls Then
        builder.MergeAttribute("controls", "controls")
    End If
    Dim tilestring = New HtmlString(builder.ToString())
    
    @tilestring
    
End Helper

